#include "Super_Man.h"
#include "PowderGame.h"
#include <fstream>

Super_Man::Super_Man(int Width, int Height) : screen(sf::VideoMode(Width,Height,32), "Openpowder"),
		info(this), background(NULL){
	needResizing=false;
	initialize();
}
void Super_Man::initialize(){
	screen.SetActive(true);
	std::ifstream fload("font.txt");
	std::string fname;
	if(fload && std::getline(fload, fname) && font.LoadFromFile(fname, 16)){
		printf("Succesfully loaded font %s\n", fname.c_str());
	}else{
		printf("Unable to load font, using default\n");
		font = sf::Font::GetDefaultFont();
	}
	currentView = screen.GetDefaultView();
	screen.SetView(currentView);
	//screen.UseVerticalSync(true);
	info.SetFonts();
}
void Super_Man::handleEvents(){
	while(screen.GetEvent(event)){
		switch(event.Type){
		case sf::Event::KeyPressed:
		case sf::Event::KeyReleased:
			handleKeys();
			break;
		case sf::Event::Resized:
			//let the game know
			needResizing=true;
			sizeEvent=event.Size;
			break;
		case sf::Event::TextEntered:
			//implement cli?
			break;
		case sf::Event::Closed:
			screen.Close();
			break;
		}
	}
	if(needResizing){
		needResizing=false;
		resizeEvent();
	}
}
void Super_Man::handleKeys(){
	switch(event.Key.Code){
	case sf::Key::Escape:
		screen.Close();
		break;
	}
}
void Super_Man::step(){
	info.update(screen.GetInput(), screen.GetFrameTime());
	if(background)
		background->update(screen.GetInput(), screen.GetFrameTime());
}
void Super_Man::draw(){
	screen.Clear();
	if(background){
		//screen.Draw(*background);
		((GameManager*)background)->forceRender(screen);
	}
	screen.Draw(info);
	screen.Display();
}

int Super_Man::main(){
	int retval=0;
	while(screen.IsOpened()){
		handleEvents();
		step();
		draw();
	}
	return retval;
}
void Super_Man::printLines(const std::string& lines, float left, float top){
	sf::String cString;
	cString.SetFont(font);
	cString.SetSize(font.GetCharacterSize());
	cString.SetText(lines);
	cString.SetX(left);
	float yPos = (top < 0 ? screen.GetHeight() : 0) + top*font.GetCharacterSize();
	cString.SetY(yPos);
	screen.Draw(cString);
}
void Super_Man::printLines(const std::stringstream& lines, float left, float top){
	printLines(lines.str(), left, top);
}
void Super_Man::flipY(bool flip){
	//assumes that the gl state is more or less normal
	glLoadIdentity();
	if(flip){
		glTranslatef(0.f,screen.GetHeight(),0.f);
		glScalef(1.f,-1.f,1.f);
	}
}
const sf::Font& Super_Man::GetFont() const{return font;}
void Super_Man::setBackground(Super_Widget* bg){
	background = bg;
	if(background)
		background->SetSize(vec2f(screen.GetWidth(), screen.GetHeight()));
}

void Super_Man::resizeEvent(){
	//printf("Resizing to %u %u\n", sizeEvent.Width, sizeEvent.Height);
	currentView.SetFromRect(sf::FloatRect(0,0,sizeEvent.Width,sizeEvent.Height));
	if(background)
		background->SetSize(vec2f(sizeEvent.Width, sizeEvent.Height));
}

Super_Man::InfoWidget::InfoWidget(Super_Man* hero) : Text_Widget(hero){
	SetColor(sf::Color(127,127,127,127));
}
void Super_Man::InfoWidget::update(){
	std::stringstream ss;
	ss << "FPS: " << 1.f/myHero->screen.GetFrameTime() << '\n';
	ss << "Size: " << myHero->screen.GetWidth()
			<< " x " << myHero->screen.GetHeight() << '\n';
	ss << "Mouse Position: " << myHero->screen.GetInput().GetMouseX()
			<< " x " << myHero->screen.GetInput().GetMouseY();
	SetText(ss.str());
}
void Super_Man::InfoWidget::update(const sf::Input& sfi, float time){
	update();
	if(IsInside(vec2f(sfi.GetMouseX(), sfi.GetMouseY())))
		SetColor(sf::Color(255,0,0,127));
	else
		SetColor(sf::Color(127,127,127,127));
}



